package com.bjpowernode.money.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.bjpowernode.money.constant.IndexConstant;
import com.bjpowernode.money.mapper.FinanceAccountMapper;
import com.bjpowernode.money.mapper.UserMapper;
import com.bjpowernode.money.model.FinanceAccount;
import com.bjpowernode.money.model.User;
import com.bjpowernode.money.service.UserService;
import com.bjpowernode.money.utils.ThreadPoolManager;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @author ZHAOZHENYU
 * @since 2022/10/14
 */

@Service(interfaceClass = UserService.class,version = "1.0.0",timeout = 20000)
@Component
public class UserServiceImpl implements UserService {

    @Autowired(required = false)
    private RedisTemplate redisTemplate;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private FinanceAccountMapper financeAccountMapper;

    @Override
    public Long queryUserCount() {
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        Long userCount = (Long) redisTemplate.opsForValue().get(IndexConstant.USER_COUNT);
        if (userCount == null){
            synchronized (this) {
                // 再查一次是否已经放进redis
                userCount = (Long) redisTemplate.opsForValue().get(IndexConstant.USER_COUNT);
                if (userCount == null) {
                    userCount = userMapper.selectUserCount();
                }
                redisTemplate.opsForValue().set(IndexConstant.USER_COUNT, userCount,5, TimeUnit.MINUTES);
            }
        }
        return userCount;
    }

    @Override
    public int checkPhone(String phone) {
        return userMapper.selectCountByPhone(phone);
    }

    @Override
    public User regUser(String phone, String loginPassword) {
        User user = new User();
        user.setPhone(phone);
        user.setLoginPassword(loginPassword);
        user.setAddTime(new Date());
        // 插入后返回主键给user
        int num = userMapper.insertSelective(user);
        if (num == 1) {
            ThreadPoolManager.getThreadPoolExecutor().execute(new Runnable() {
                @Override
                public void run() {
                    FinanceAccount financeAccount = new FinanceAccount();
                    financeAccount.setAvailableMoney(888d);
                    financeAccount.setUid(user.getId());
                    financeAccountMapper.insertSelective(financeAccount);
                }
            });
            return user;
        }
        return null;
    }

    @Override
    public boolean checkPhoneById(String phone, Integer userId) {
        String odlPhone = userMapper.selectPhoneById(userId);
        if (StringUtils.equals(phone,odlPhone)){
            return true;
        }
        return false;
    }

    @Override
    public int updateById(User user) {
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public User loginUser(String phone, String loginPassword) {

        User user = userMapper.selectUserByPhoneAndPwd(phone, loginPassword);

        if (ObjectUtils.allNotNull(user)) {
            // 异步更新登录时间
            ThreadPoolManager.getThreadPoolExecutor().execute(new Runnable() {
                @Override
                public void run() {
                    User user_d = new User();
                    user_d.setLastLoginTime(new Date());
                    user_d.setId(user.getId());
                    userMapper.updateByPrimaryKeySelective(user_d);
                }
            });
            // 返回用户
            return user;
        }

        return null;
    }

    @Override
    public int updateHeaderById(String newFileName, Integer id) {
        User user = new User();
        user.setId(id);
        user.setHeaderImage(newFileName);
        return userMapper.updateByPrimaryKeySelective(user);
    }
}
